package com.lagou.email.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private JavaMailSender sender;

    @Value("${spring.mail.username}")
    private String mailFrom;

    @GetMapping("/{email}/{code}")
    public boolean sendCode2EmailAddr(@PathVariable("email") String email, @PathVariable("code") String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mailFrom);
        message.setTo(email);
        message.setSubject("验证码");
        message.setText(code);
        try {
            sender.send(message);
            return true;
        } catch (Exception e) {
            return false;
        }

    }
}
